<template>
  <div class="product-list-wrap">
    <div class="product-list-content">
      <header class="category-header wrap">
        <i class="iconfont icon-fanhui1" @click="goBack"></i>
        <div class="header-search">
          <i class="iconfont icon-sousuo"></i>
          <input type="text" class="search-title" v-model="keyword" />
        </div>
        <span class="search-btn" @click="getSearch">搜索</span>
      </header>
      <van-tabs type="card" color="#1baeae" @click="changeTab">
        <van-tab title="推荐" name="">
          <van-pull-refresh class="product-list-refresh" v-model="refreshing" @refresh="onRefresh">
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" @offset="300">
              <div class="product-item" v-for="(item, index) in productList" :key="index" @click="productDetail(item)">
                <img :src="item.goodsCoverImg" />
                <div class="product-info">
                    <p class="name">{{ item.goodsName }}</p>
                    <p class="subtitle">{{ item.goodsIntro }}</p>
                    <span class="price">￥ {{ item.sellingPrice }}</span>
                </div>
              </div>
            </van-list>
          </van-pull-refresh>
        </van-tab>
        <van-tab title="新品" name="new"></van-tab>
        <van-tab title="价格" name="price"></van-tab>
      </van-tabs>
    </div>
  </div>
</template>
  
  <!-- <script setup lang="ts">
  import { onMounted, ref } from 'vue'
  import { useRouter } from 'vue-router'
  import { search } from '../service/good'
  interface ProductListItem {
      goodsId: number,
      goodsName: string,
      goodsIntro: string,
      goodsCoverImg: string,
      sellingPrice: number
  }
  const router = useRouter()
  const refreshing = ref<boolean>(false)
  const loading = ref<boolean>(true)
  const finished = ref<boolean>(false)
  const productList = ref<ProductListItem []>([])
  const page = ref<number>(1)
  const totalPage = ref<number>(0)
  const categoryId = ref<number>(123)
  const keyword = ref<string>('口红')
  const orderBy = ref('')
  // 初始化列表数据
  async function init () {
    const {
      data,
      data: { list }
    } = await search({
      pageNumber: page,
      goodsCategoryId: categoryId,
      keyword: keyword,
      orderBy: orderBy
    })
    productList.value = productList.value.concat(list)
    console.log(productList.value)
    totalPage.value = data.totalPage
    loading.value = true
    if (page.value > totalPage.value) {
      finished.value = true
    }
  }
  function goBack () {
    router.go(-1)
  }
  function productDetail (item:any) {
    router.push({ path: `product/${item.goodsId}` })
  }
  function onLoad () {
    if (!refreshing.value && page.value < totalPage.value) {
      page.value = page.value + 1
    }
    if (refreshing.value) {
      productList.value = []
      refreshing.value = false
    }
    init()
  }
  function onRefresh () {
    refreshing.value = true
    loading.value = false
    finished.value = false
    page.value = 1
    onLoad()
  }
  function getSearch () {
    onRefresh()
  }
  function changeTab (name :string, title :string) {
    orderBy.value = name
    onRefresh()
  }
  onMounted(() => {
    init()
  })
  </script> -->
  
  <style lang="less" scoped>
  @import '../common/style/mixin';
  .product-list-content{
      position:fixed;
      left:0;
      top:0;
      width:100%;
      z-index:1000;
      background: #fff;
      .category-header{
          .fj();
          width:100%;
          height:50px;
          line-height:50px;
          .boxSizing();
          padding:0 15px;
          font-size: 15px;
          color:#656771;
          z-index:10000;
          &.active{
              background:@primary;
          }
          .icon-fanhui1{
              font-size: 25px;
              font-weight: bold;
          }
          .header-search{
              display: flex;
              width:76%;
              height:20px;
              line-height:20px;
              padding:5px 0;
              margin:10px 0;
              color:#232326;
              background: #f7f7f7;
              .borderRadius(20px);
              .icon-sousuo{
                  padding:0 5px 0 20px;
                  font-size:17px;
              }
              .search-title {
                  font-size: 12px;
                  color: #666;
                  background: #f7f7f7;
              }
          }
          .search-btn {
              height: 28px;
              margin: 8px 0;
              line-height: 28px;
              padding: 0 5px;
              color: #fff;
              background: @primary;
              .borderRadius(5px);
              margin-top: 10px;
          }
      }
  }
  .product-list-refresh{
      margin-top:78px;
      .product-item {
      .fj();
      width: 100%;
      height: 120px;
      padding: 10px 0;
      border-bottom: 1px solid #dcdcdc;
      img {
        width: 140px;
        height: 120px;
        padding: 0 10px;
        .boxSizing();
      }
      .product-info {
        width: 56%;
        height: 120px;
        padding: 5px;
        text-align: left;
        .boxSizing();
        p {
          margin: 0;
        }
        .name {
          width: 100%;
          max-height: 40px;
          line-height: 20px;
          font-size: 15px;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .subtitle {
          width: 100%;
          max-height: 20px;
          padding: 10px 0;
          line-height: 25px;
          font-size: 13px;
          color: #999;
          overflow: hidden;
        }
        .price {
          color: @primary;
          font-size: 16px;
        }
      }
    }
  }
  </style>
  